package com.star.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.pojo.Type;
import com.star.service.impl.BlogServiceImpl;
import com.star.service.impl.TypeServiceImpl;
import com.star.vo.FirstPageBlog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * com.star.controller
 *
 * @Description: 分类展示控制器
 * @Date: 2020-08-11 15:00
 * @Author: LENSTAR
 * @URL: http://123.57.161.114:9000/
 */
@Controller
public class TypeShowController {

    @Autowired
    private TypeServiceImpl typeService;
    @Autowired
    private BlogServiceImpl blogService;

    @GetMapping("types/{id}")
    public String types(@PathVariable("id") Long id, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, Model model) {
        List<Type> types = typeService.getAllTypeAndBlog();

        //id=-1表示从导航栏进入分类页面
        if (id == -1) {
            id = types.get(0).getId();//默认为第一个分类
        }
        model.addAttribute("types", types);
        List<FirstPageBlog> blogList = blogService.getByTypeId(id);
        PageHelper.startPage(pageNum,10,"create_time desc");
        PageInfo<FirstPageBlog> pageInfo = new PageInfo<>(blogList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}
